package com.xbongbong.saas.service;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.PaySheetTypeEnum;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.dto.AddBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.AfterSavePojo;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainInLinkItemDTO;
import com.xbongbong.pro.listbatch.pojo.dto.AddLabelDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.PayPlanSheetDeleteBatchDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.PayPlanSheetInsertBatchDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.PayPlanSheetInsertDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.PaySheetEditAttrUpdateDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.PaySheetUpdateBatchDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.batch.PayPlanSheetDTO;
import com.xbongbong.pro.payplansheet.pojo.vo.PayPlanSheetDeleteBatchVO;
import com.xbongbong.pro.payplansheet.pojo.vo.PayPlanSheetInsertBatchVO;
import com.xbongbong.pro.payplansheet.pojo.vo.PaySheetUpdateBatchVO;
import com.xbongbong.pro.payplansheet.pojo.vo.batch.PayPlanSheetAmountVO;
import com.xbongbong.pro.sheet.pojo.AmountDetailGetPojo;
import com.xbongbong.saas.domain.entity.ext.PayPlanSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 付款单
 *
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/23
 * @since v1.0
 */
public interface PayPlanSheetService {

    /**
     * 批量插入
     *
     * @param payPlanSheetInsertBatchDTO
     * @return com.xbongbong.pro.payplansheet.pojo.vo.PaymentSheetInsertBatchVO
     * @throws XbbException 业务异常
     * @author 吴峰
     * @date 2019/3/7 20:56
     */
    PayPlanSheetInsertBatchVO insertBatch(PayPlanSheetInsertBatchDTO payPlanSheetInsertBatchDTO, String processCreatorId) throws XbbException;

    /**
     * 批量删除
     *
     * @param payPlanSheetDeleteBatchDTO
     * @return com.xbongbong.pro.payplansheet.pojo.vo.PayPlanSheetDeleteBatchVO
     * @throws XbbException 业务异常
     * @author 吴峰
     * @date 2019/3/7 20:56
     */
    PayPlanSheetDeleteBatchVO deleteBatch(PayPlanSheetDeleteBatchDTO payPlanSheetDeleteBatchDTO) throws XbbException;

    /**
     * 保存付款单
     *
     * @param payPlanSheetInsertDTO 入参
     * @param paasFormDataEntity 数据实体
     * @param paasFormEntityExt 表单实体
     * @return 是否成功
     * @throws XbbException 异常
     */
    FormDataAddVO add(PayPlanSheetInsertDTO payPlanSheetInsertDTO, PaasFormDataEntity paasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException;

    /**
     * 新建时格式化解释（参照一期处理）
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException;

    /**
     * 关联新建核销预付款
     * @param handlerExplainInLinkItemDTO 入参
     * @throws XbbException 异常
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void formatLinkAddWriteOffPrepay(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException;

    /**
     * 关联新建红冲预付款余额付款单
     * @param handlerExplainInLinkItemDTO 入参
     * @throws XbbException 异常
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void formatLinkAddRedPrepayment(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException;


    void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException;

    /**
     * 得到原单的已红冲总金额，并查询原单关联的已红冲单据封装成purchaseId_payPlanId 和 红冲金额的对应关系map（如果有子单则得到子单的对应关系）
     * 注：此方法未对红冲金额求正值，因此此时的红冲金额还为负数
     * @param corpid 公司id
     * @param sheetId 原单id
     * @param amountDetailGetPojo 获取金额明细时需要用到的条件、原单信息pojo
     * @return java.util.Map<java.lang.String,java.lang.Double>
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    Map<String, Double> handleRedSheetsBySheet(String corpid, Long sheetId, AmountDetailGetPojo amountDetailGetPojo) throws XbbException;

    /**
     * 批量保存前置
     * @return
     * @throws XbbException
     * @param formDataSaveDTO
     * @param paySheetTypeEnum 付款单类型
     * @param supplierId
     * @author
     */
    AddBatchDTO beforeSaveBatch(FormDataAddDTO formDataSaveDTO, PaySheetTypeEnum paySheetTypeEnum, Long supplierId) throws XbbException;

    /**
     * 更新回滚es
     * @param idIn
     * @param corpid
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void updateRollback(Collection<Long> idIn, String corpid) throws XbbException;

    /**
     * 批量保存回款单
     * @param payPlanSheetDTO
     * @param paySheetTypeEnum
     * @param supplierId
     * @return
     * @throws XbbException
     */
    PayPlanSheetAmountVO addBatch(PayPlanSheetDTO payPlanSheetDTO, PaySheetTypeEnum paySheetTypeEnum, Long supplierId) throws XbbException;
    /**
     * 批量保存回款单(工作流)
     * @param payPlanSheetDTO
     * @param paySheetTypeEnum
     * @param supplierId
     * @return
     * @throws XbbException
     */
    PayPlanSheetAmountVO addBatchForWorkflow(PayPlanSheetDTO payPlanSheetDTO, PaySheetTypeEnum paySheetTypeEnum, Long supplierId) throws XbbException;

    /**
     * 批量更新-只更新特定字段
     * @param paySheetUpdateBatchDTO
     * @return com.xbongbong.pro.payplansheet.pojo.vo.PaySheetUpdateBatchVO
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    PaySheetUpdateBatchVO updateAttrBatch(PaySheetUpdateBatchDTO paySheetUpdateBatchDTO) throws XbbException;
    PaySheetUpdateBatchVO updateAttrBatch(PaySheetUpdateBatchDTO paySheetUpdateBatchDTO, boolean needThrow) throws XbbException;

    /**
     * 列表页批量编辑
     * @param paySheetEditAttrUpdateDTO
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void batchEditAttr(PaySheetEditAttrUpdateDTO paySheetEditAttrUpdateDTO) throws XbbException;
    /**
     * 批量获取用户下的红冲预收款和预收款回款单
     * @param corpid
     * @param supplierIds
     * @param typeList
     * @return
     * @throws XbbException
     */
    List<PaasFormDataEntityExt> getPaymentSheetEsDataListBySupplierIds(String corpid, List<Long> supplierIds, List<String> typeList) throws XbbException;

    /**
     * 格式化移动端列表页
     *
     * @param formDataListDTO 入参
     * @param esEntities 数据列表
     * @param totalCount 数据个数
     * @return com.xbongbong.pro.applist.pojo.vo.ListAppVO
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/3 14:15
     * @since v1.0
     */
    ListAppVO formatPaySheetAppList(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt> esEntities, Integer totalCount) throws XbbException;

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO 导入入参
     * @param titlePojoList     标题
     * @param rowGroupPojo      一个表单对应的一组Excel填写的数据
     * @param fieldAttrList     解释
     * @return com.xbongbong.paas.pojo.dto.FormDataAddDTO
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 计算付款单已红冲的总金额
     * @param corpid 公司ID
     * @param sheetIdIn 原付款单ID集合
     * @return 付款单ID和已红冲金额关系map
     * @throws XbbException error
     * @author zhouwq
     * @date 2020/3/6 15:14
     */
    Map<Long, Double> sumRedAmountByPaySheetIdIn(String corpid, List<Long> sheetIdIn) throws XbbException;
    
    /**
     * 统计该类型的金额
     * @param corpid
     * @param sheetId
     * @param codeList
     * @return
     * @throws XbbException
     */
    double sumAmountBySheet(String corpid, Long sheetId, List<String> codeList) throws XbbException;

    /**
     * 一致性同步
     * @param corpid 公司ID
     * @param supplierId 供应商id
     * @param supplierName 供应商名称
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void dataConsistencyUpdateSupplier(String corpid, Long supplierId, String supplierName) throws XbbException;

    /**
     * 一致性同步
     * @param corpid 公司ID
     * @param purchaseId 采购合同id
     * @param serialNo 采购合同编号
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void dataConsistencyUpdatePurchase(String corpid, Long purchaseId, String serialNo) throws XbbException;

    /**
     * 批量增删标签
     * @param addLabelDTO
     * @param addBatchList
     */
    void updateBatchLabelAttr(AddLabelDTO addLabelDTO, List<PaasFormDataEntityExt> addBatchList, Boolean addOrDelete) throws XbbException;

    /**
     * 回款单保存后逻辑
     * @param saasFormSaveDTO saas保存数据
     * @throws XbbException 业务异常
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    void afterSave(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    /**
     * 工作流付款单团队保存
     *
     * @param saasFormSaveDTO
     */
    void afterSaveForWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    /**
     * 操作回款单团队
     *
     * @param payPlanSheetEntityExt
     * @param sheetList
     * @param ignoreMainSheet
     * @param paasFormDataEntity
     * @param saasNeedRedundantAttrPojo
     * @param isImport
     * @throws XbbException
     */
    AfterSavePojo operateSheetTeam(PayPlanSheetEntityExt payPlanSheetEntityExt, List<PayPlanSheetEntityExt> sheetList,
                                   boolean ignoreMainSheet, PaasFormDataEntity paasFormDataEntity, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo, Integer isImport, Boolean workFlowTag) throws XbbException;

    void afterSaveForUpdateWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;
}
